package com.swxy.club.controller;



import com.swxy.club.dto.Vo.ClubMemberVO;
import com.swxy.club.dto.Vo.ManagedClubVO;
import com.swxy.club.dto.Vo.UserClubVO;
import com.swxy.club.dto.dt.ClubMemberPageQuery;
import com.swxy.club.dto.dt.ClubQueryDTO;
import com.swxy.club.dto.dt.ClubRegisterDTO;
import com.swxy.club.dto.bo.CurrentUserBO;
import com.swxy.club.dto.dt.UpdateMemberRoleDTO;
import com.swxy.club.result.Result;
import com.swxy.club.service.ClubService;
import com.swxy.club.utils.UserContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;

@RestController
@RequestMapping("/api/clubs")
@Api(tags = "社团管理接口")
@RequiredArgsConstructor
public class ClubController {

    private final ClubService clubService;

    /**
     * 注册社团
     * @param dto
     * @return
     */
    @PostMapping("/register")
    @ApiOperation("注册新社团")
    public Result registerClub(@RequestBody ClubRegisterDTO dto)
    {
        clubService.registerClub(dto);
        return Result.success("注册成功");
    }

    /**
     * 分页查询社团列表
     * @param queryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("分页查询社团列表")
    public Result getClubPage( ClubQueryDTO queryDTO) {
        return Result.success(clubService.getClubPage(queryDTO));
    }

    /**
     * 根据ID获取社团详情
     * @param clubId
     * @return
     */
    @GetMapping("/{clubId}")
    @ApiOperation("根据ID获取社团详情")
    public Result getClubById(@PathVariable("clubId") String clubId) {

        return Result.success(clubService.getUserByClubIdNumber(clubId));
    }

    /**
     * 修改社团状态
     * @param clubId
     * @param status
     * @return
     */
    @PutMapping("/{clubId}/status")
    @ApiOperation("修改社团状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "clubId", value = "社团ID", required = true, paramType = "path"),
            @ApiImplicitParam(name = "status", value = "目标状态(active/paused/disbanded)", required = true, paramType = "query")
    })
    public Result updateClubStatus(
            @PathVariable("clubId") Long clubId,
            @RequestParam @NotBlank String status
           ) {

        clubService.updateClubStatus(clubId, status);
        return Result.success("社团状态更新成功");
    }

    /**
     * 直接加入社团
     * @param clubId

     * @return
     */
    @PostMapping("/join")
    @ApiOperation("直接加入社团")
    @ApiImplicitParam(name = "clubId", value = "社团ID", required = true, example = "1")
    public Result joinClub(@RequestParam Long clubId)
    {

        clubService.joinClub(clubId);
        return Result.success("成功加入社团");
    }

    /**
     * 查询社团成员列表
     * @param clubId
     * @return
     */
    @GetMapping("/{clubId}/members")
    @ApiOperation("查询社团成员列表")
    @ApiImplicitParam(name = "clubId", value = "社团ID", required = true, example = "1")
    public Result getClubMembers(@PathVariable Long clubId) {
        List<ClubMemberVO> members = clubService.getClubMembers(clubId);
        return Result.success(members);
    }

    @ApiOperation(value = "分页查询社团成员")
    @GetMapping("/members/page")
    public Result getClubMembersByPage(ClubMemberPageQuery query) {
        return Result.success(clubService.getClubMembersByPage(query));

    }
    /**
     * 删除社团成员
     */
    @DeleteMapping("/{clubId}/members/{userId}")
    @ApiOperation("删除社团成员")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "clubId", value = "社团ID", required = true, example = "1"),
            @ApiImplicitParam(name = "userId", value = "用户ID", required = true, example = "1")
    })
    public Result deleteClubMember(@PathVariable Long clubId, @PathVariable Long userId) {
        clubService.deleteClubMember(clubId, userId);
        return Result.success("删除成功");
    }
    /**
     * 查看社团成员详细信息
     */
    @GetMapping("/{clubId}/members/{userId}")
    @ApiOperation("查看社团成员详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "clubId", value = "社团ID", required = true, example = "1"),
            @ApiImplicitParam(name = "userId", value = "用户ID", required = true, example = "1")
    })
    public Result getClubMember(@PathVariable Long clubId, @PathVariable Long userId) {
        ClubMemberVO member = clubService.getClubMember(clubId, userId);
        return Result.success(member);
    }

    /**
     * 修改社团成员角色
     */
    @PutMapping("/members/role")
    @ApiOperation("修改社团成员角色")
    public Result updateClubMemberRole(@RequestBody @Valid UpdateMemberRoleDTO dto) {
        clubService.updateClubMemberRole(dto);
        return Result.success("修改成功");
    }

    /**
     *
     * 获取当前用户加入的所有社团
     */
    @GetMapping("/my-clubs")
    @ApiOperation("获取当前用户加入的所有社团")
    public Result getMyClubs() {
        CurrentUserBO currentUser = UserContext.getCurrentUser();
        Long currentUserId = currentUser.getId();
        List<UserClubVO> myClubs = clubService.getClubsByUserId(currentUserId);
        return Result.success(myClubs);
    }

    /**
     * 获取当前用户管理的社团
     * @return
     */
    @GetMapping("/managed-clubs")
    @ApiOperation("获取当前用户管理的社团")
    public Result getManagedClubs() {
        CurrentUserBO currentUser = UserContext.getCurrentUser();
        Long currentUserId = currentUser.getId();
        List<ManagedClubVO> managedClubs = clubService.getManagedClubsByUserId(currentUserId);
        return Result.success(managedClubs);
    }


}

